<template>
  <div class="conntainer">

    <ul class="bubble-bgwall">
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
    </ul>
    <center>
      <el-steps class="mb-4" style="max-width: 600px; margin: 0 auto" :space="200" :active="active"
        finish-status="success" simple>
        <el-step title="邮箱验证" :icon="Message" />
        <el-step title="修改密码" :icon="Edit" />
        <el-step title="完成" :icon="SuccessFilled" />
      </el-steps>

      <!-- 邮箱验证 -->
      <div v-if="active === 0" class="form-wrapper">
        <h1 class="welcomeFont">邮箱验证</h1>
        <el-form ref="formRef" :model="verifyEmailForm" :rules="rules" label-width="auto"
          :label-position="labelPosition" style="max-width: 300px; padding: 10px 20px">
          <el-form-item label="邮箱" prop="email" class="label">
            <el-input v-model="verifyEmailForm.email" clearable placeholder="请输入邮箱" />
          </el-form-item>
          <el-row>
            <el-col :span="16">
              <el-form-item label="验证码" prop="captcha" width="150px" class="label">
                <el-input v-model="verifyEmailForm.captcha" placeholder="请输入验证码" />
              </el-form-item>
            </el-col>
            <el-col :span="5" style="margin-left: 3px">
              <el-button round @click="getCode" :disabled="isDisposed">{{
                isDisposed ? `${time}s后重新获取` : "获取验证码"
              }}</el-button>
            </el-col>
          </el-row>
          <el-form-item>
            <el-button type="primary" round class="btu" @click="verifyEmailSubmit" :loading="butLoading">提 交</el-button>
          </el-form-item>
        </el-form>
      </div>

      <!-- 修改密码 -->
      <div v-if="active === 1" class="form-wrapper">
        <h1 class="welcomeFont">修改密码</h1>
        <el-form ref="formRef" :model="modPasswordForm" :rules="modPasswrodRules" label-width="auto"
          :label-position="labelPosition" style="max-width: 300px; padding: 10px 20px">
          <el-form-item label="新密码" prop="password" class="label">
            <el-input v-model="modPasswordForm.password" type="password" show-password autocomplete="off" clearable
              placeholder="请输入新密码" />
          </el-form-item>
          <el-form-item label="确认密码" prop="confirmPassword" class="label">
            <el-input v-model="modPasswordForm.confirmPassword" type="password" show-password autocomplete="off"
              clearable placeholder="请再次输入新密码" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" round class="btu" @click="modPassword" :loading="modPasswordLoading">修
              改</el-button>
          </el-form-item>
        </el-form>
      </div>

      <!-- 修改成功之后的成功界面 -->
      <div v-if="active === 2" style="margin-top: 23vh;">
        <el-result icon="success" title="修改密码成功" sub-title="请前往登录界面，重新登录吧">
          <template #extra>
            <button class="button" @click="goLogin"><span>go </span></button>
          </template>
        </el-result>
      </div>
    </center>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import { Edit, SuccessFilled, Message } from "@element-plus/icons-vue";
import { ElMessage, FormProps } from "element-plus";
import { sendCode } from "@/api/register";
import { emailVerification, updatePassword } from "@/api/forgetPassword";
import router from "@/router";

const active = ref(0);

const next = () => {
  if (active.value++ > 2) active.value = 0;
};

//邮箱验证
//表单对齐方式
const labelPosition = ref<FormProps["labelPosition"]>("left");
const formRef = ref(null);

interface verifyEmailData {
  email: string;
  captcha: string;
}

const verifyEmailForm: verifyEmailData = reactive({
  email: "",
  captcha: "",
});

//表单校验规则
const rules = reactive({
  email: [
    { message: "请输入您的邮箱", trigger: "blur" },
    {
      type: "string",
      pattern: /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/,
      message: "请输入正确的邮箱地址",
      trigger: "blur",
    },
  ],
  captcha: [{ message: "请输入验证码", trigger: "blur" }],
});

//倒计时效果
const time = ref(60);
const isDisposed = ref(false);
const handleTimeChange = () => {
  if (time.value <= 0) {
    isDisposed.value = false;
    time.value = 60;
  } else {
    setTimeout(() => {
      time.value--;
      handleTimeChange();
    }, 1000);
  }
};
//获取验证码
const getCode = async () => {
  verifyEmailForm.captcha = "";
  const res = await sendCode(verifyEmailForm.email);
  if (res.code === 200) {
    ElMessage({
      type: "success",
      message: res.msg,
    });
    ElMessage({
      type: "success",
      message: "验证码在一分钟内有效",
    });
    //开始倒计时
    isDisposed.value = true;
    handleTimeChange();
  } else {
    ElMessage.error(res.msg);
  }
};

//提交按钮的加载状态
const butLoading = ref<boolean>(false);
//邮箱验证的提交按钮
const verifyEmailSubmit = async () => {
  //加载中
  butLoading.value = true;
  const res = await emailVerification(verifyEmailForm);
  if (res.code === 200) {
    ElMessage({
      type: "success",
      message: res.msg,
    });
    butLoading.value = false;
    //验证成功，执行下一步
    next();
  } else {
    ElMessage.error(res.msg);
    butLoading.value = false;
    return;
  }
};

//修改密码
//修改密码表单
const modPasswordForm = reactive({
  password: "",
  confirmPassword: "",
});
//修改密码表单规则
const modPasswrodRules = reactive({
  password: [{ message: "请输入您的新密码", trigger: "blur" }],
  confirmPassword: [
    { message: "请再次输入您的新密码", trigger: "blur" },
  ],
});
//修改密码的按钮加载状态
const modPasswordLoading = ref<boolean>(false);
//修改密码
const modPassword = async () => {
  if (modPasswordForm.password === modPasswordForm.confirmPassword) {
    //加载中
    modPasswordLoading.value = true;
    const res = await updatePassword(modPasswordForm);
    if (res.code === 200) {
      ElMessage({
        type: "success",
        message: res.msg,
      });
      modPasswordLoading.value = false;
      //验证成功，执行下一步
      next();
    } else {
      ElMessage.error(res.msg);
      modPasswordLoading.value = false;
      return;
    }
  } else {
    ElMessage.error("您输入的两次密码不一致，请重新输入");
  }
};

//前往登录界面
const goLogin = () => {
  router.push('/login')
}
</script>

<style lang="scss" scoped>
//最外层盒子
.conntainer {
  height: 100%;
  // background-image: url("@/assets/img/registrationBackgroundImage.jpg");
  background: linear-gradient(to bottom, #F5F4F1, #3F5B62);
  background-repeat: no-repeat;
  background-size: cover;

  .bubble-bgwall {
    overflow: hidden;
    position: fixed;
    margin: 0 auto;
    width: 100%;
    height: 100%;
    padding: 0;
  }

  .bubble-bgwall li {
    display: flex;
    position: absolute;
    bottom: -200px;
    justify-content: center;
    align-items: center;
    border-radius: 10px;
    width: 50px;
    height: 50px;
    background-color: rgba(255, 255, 255, 0.15);
    color: #ccc;
    animation: bubble 15s infinite;
    z-index: 999;
  }

  .bubble-bgwall li:nth-child(1) {
    left: 10%;
  }

  .bubble-bgwall li:nth-child(2) {
    left: 20%;
    width: 90px;
    height: 90px;
    animation-duration: 7s;
    animation-delay: 2s;
  }

  .bubble-bgwall li:nth-child(3) {
    left: 25%;
    animation-delay: 4s;
  }

  .bubble-bgwall li:nth-child(4) {
    left: 40%;
    width: 60px;
    height: 60px;
    background-color: rgba(255, 255, 255, 0.3);
    animation-duration: 8s;
  }

  .bubble-bgwall li:nth-child(5) {
    left: 70%;
  }

  .bubble-bgwall li:nth-child(6) {
    left: 80%;
    width: 120px;
    height: 120px;
    background-color: rgba(255, 255, 255, 0.2);
    animation-delay: 3s;
  }

  .bubble-bgwall li:nth-child(7) {
    left: 32%;
    width: 160px;
    height: 160px;
    animation-delay: 2s;
  }

  .bubble-bgwall li:nth-child(8) {
    left: 55%;
    width: 40px;
    height: 40px;
    font-size: 12px;
    animation-duration: 15s;
    animation-delay: 4s;
  }

  .bubble-bgwall li:nth-child(9) {
    left: 25%;
    width: 40px;
    height: 40px;
    background-color: rgba(255, 255, 255, 0.3);
    font-size: 12px;
    animation-duration: 12s;
    animation-delay: 2s;
  }

  .bubble-bgwall li:nth-child(10) {
    left: 85%;
    width: 40px;
    height: 40px;
    animation-delay: 5s;
  }

  @keyframes bubble {
    0% {
      transform: translateY(0) rotate(45deg);
    }

    100% {
      transform: translateY(-1000px) rotate(180deg);
    }
  }

  //表单样式
  .form-wrapper {
    position: relative;
    width: 400px;
    height: 250px;
    padding-bottom: 35px;
    margin-top: 20vh;
    border-radius: 5%;
    // background-color: rgba(255, 255, 255, 0.13);
    background: linear-gradient(to bottom right,
        #F5F4F1,
        #3F5B62,
      );

    //欢迎词语
    .welcomeFont {
      // padding: 20px 10px;
      padding-top: 20px;
      font-size: 40px;
      color: #fefefe;
      text-shadow: 0px 1px 0px #c0c0c0, 0px 2px 0px #b0b0b0, 0px 3px 0px #a0a0a0,
        0px 4px 0px #909090, 0px 5px 10px rgba(0, 0, 0, 0.9);
    }

    //登录按钮
    .btu {
      padding: 20px 135px;
      left: 8px;
      position: absolute;
      top: 0;
    }
  }
}

.button {
  display: inline-block;
  border-radius: 80px;
  background-color: #0d2286;
  border: none;
  color: #FFFFFF;
  text-align: center;
  font-size: 28px;
  padding: 20px;
  width: 150px;
  transition: all 0.5s;
  cursor: pointer;
  margin: 5px;
  vertical-align: middle;
}

.button span {
  cursor: pointer;
  display: inline-block;
  position: relative;
  transition: 0.5s;
}

.button span:after {
  content: '»';
  position: absolute;
  opacity: 0;
  top: 0;
  right: -20px;
  transition: 0.5s;
}

.button:hover span {
  padding-right: 25px;
}

.button:hover span:after {
  opacity: 1;
  right: 0;
}

.label {
  background: #8CA9AA;
  border-radius: 7px;
  padding-left: 10px;
}

:deep(.el-result__title p) {
  color: #ffffff;
  font-size: var(--el-result-title-font-size);
  line-height: 1.3;
  margin: 0;
}

:deep(.el-checkbox__input.is-checked .el-checkbox__inner) {
  background-color: #86969a;
  border-color: #86969a;
}

:deep(.el-checkbox__input.is-checked + .el-checkbox__label) {
  color: #86969a;
}

:deep(.el-checkbox:last-of-type) {
  margin-right: 0;
  color: #fff;
}

:deep(.el-button--primary) {
  --el-button-text-color: var(--el-color-white);
  --el-button-bg-color: #86969a;
  --el-button-border-color: #86969a;
  --el-button-outline-color: var(--el-color-primary-light-5);
  --el-button-active-color: #86969a;
  --el-button-hover-text-color: #456067;
  --el-button-hover-link-text-color: var(--el-color-primary-light-5);
  --el-button-hover-bg-color: #e3e5e3;
  --el-button-hover-border-color: #e3e5e3;
  --el-button-active-bg-color: #86969a;
  --el-button-active-border-color: #86969a;
  --el-button-disabled-text-color: var(--el-color-white);
  --el-button-disabled-bg-color: var(--el-color-primary-light-5);
  --el-button-disabled-border-color: var(--el-color-primary-light-5);
}

:deep(.el-input) {
  --el-input-text-color: var(--el-text-color-regular);
  --el-input-border: var(--el-border);
  --el-input-hover-border: var(--el-border-color-hover);
  --el-input-focus-border: #456067;
  --el-input-transparent-border: 0 0 0 1px transparent inset;
  --el-input-border-color: #fff;
  --el-input-border-radius: var(--el-border-radius-base);
  --el-input-bg-color: #fff;
  --el-input-icon-color: var(--el-text-color-placeholder);
  --el-input-placeholder-color: var(--el-text-color-placeholder);
  --el-input-hover-border-color: var(--el-border-color-hover);
  --el-input-clear-hover-color: var(--el-text-color-secondary);
  --el-input-focus-border-color: #456067;
  --el-input-width: 100%;
  --el-input-height: var(--el-component-size);
  box-sizing: border-box;
  display: inline-flex;
  font-size: var(--el-font-size-base);
  line-height: var(--el-input-height);
  position: relative;
  vertical-align: middle;
  width: var(--el-input-width);
}

:deep(.el-button:hover) {
  background-color: #456067;
  border-color: #456067;
  color: #fff;
  outline: none;
}
</style>
